<template>
	<view class="container">
		<!-- 主体内容 -->
		<view class="main-top">
			<view class="title-section">
				<text class="main-title">更加真实</text>
				<text class="sub-title">更受欢迎</text>
				<view class="security-desc">
					<text class="desc-text">1.请确认您的身份信息</text>
					<text class="required-text">*以保障您的资金账户安全</text>
				</view>
			</view>
			<view></view>
		</view>
		<view class="main-content">
			<!-- 输入区域 -->
			<view class="input-section">
				<view class="input-item required">
					<text class="star">*</text>
					<input style="width: 100%" v-model="formData.realName" placeholder="请输入真实姓名"
						placeholder-color="#999" @input="validateName" />
					<text v-if="nameError" class="error-text">{{ nameError }}</text>
				</view>
				<view class="input-item required">
					<text class="star">*</text>
					<input style="width: 100%" v-model="formData.idCardNo" placeholder="请输入身份证号码"
						placeholder-color="#999" @input="validateIdCard" />
					<text v-if="idCardError" class="error-text">{{ idCardError }}</text>
				</view>

				<view style="border-bottom: 2px solid #eee" />

				<view class="id-upload">
					<view>上传身份证正反面<span style="color: red;">(必填)</span></view>
					<view class="upload-item">
						<view class="">
							<text class="upload-label">头像面</text>
							<text class="error-text">点击上传您的身份证头像面</text>
						</view>
						<view class="">
							<u-upload @afterRead="afterRead" name="1" multiple :maxCount="1">
								<template>
									<image
										:src="formData.idCardJust ? path + formData.idCardJust : '/static/mine/Framezx.png'"
										style="width: 169.39px; height: 92.83px"></image>
								</template>
							</u-upload>
						</view>
					</view>
					<view class="upload-item">
						<view class="">
							<text class="upload-label">国徽面</text>
							<text class="error-text">点击上传您的身份证国徽面</text>
						</view>
						<view class="">
							<u-upload @afterRead="afterRead" name="2" multiple :maxCount="1">
								<template>
									<image
										:src="formData.idCardAgainst ? path + formData.idCardAgainst : '/static/mine/Framefx.png'"
										style="width: 169.39px; height: 92.83px"></image>
								</template>
							</u-upload>
						</view>
					</view>
				</view>
				<view class="id-upload" style="margin-bottom: 5vh">
					<view>上传其他证件</view>
					<view class="upload-item">
						<view class="">
							<text class="upload-label">其他</text>
							<text class="upload-span">点击上传其他证件</text>
						</view>
						<view class="">
							<u-upload @afterRead="afterRead" name="3" multiple :maxCount="1">
								<template>
									<image
										:src="formData.idCardRest ? path + formData.idCardRest : '/static/mine/Framezx.png'"
										style="width: 169.39px; height: 92.83px"></image>
								</template>
							</u-upload>
						</view>
					</view>
				</view>

				<view style="display: flex; align-items: center; justify-content: center;" @click="getAddList">
					<button>提交</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import my from '@/api/my.js';
	import col from '../../uni_modules/uview-ui/libs/config/props/col';
	import {
		idCardValidate
	} from '@/utils/validate.js';
	import api from '@/api/index.js';
	export default {
		data() {
			return {
				formData: {
					realName: '',
					idCardNo: '',
					idCardJust: '',
					idCardAgainst: '',
					idCardRest: ''
				},
				nameError: '',
				idCardError: '',
				baseUrl: api.getPathApi(),
				path: api.getPath()
			};
		},
		computed: {
			isFormValid() {
				return !this.nameError && !this.idCardError && this.formData.realName && this.formData.idCardNo;
			}
		},
		methods: {
			//验证
			// 姓名验证（2-4个中文字符）
			validateName() {
				const value = this.formData.realName.trim();
				const regex = /^[\u4e00-\u9fa5]{2,4}$/;

				if (!value) {
					this.nameError = '姓名不能为空';
				} else if (!regex.test(value)) {
					this.nameError = '请输入2-4个中文字符';
				} else {
					this.nameError = '';
				}
			},
			validateIdCard() {
				const value = this.formData.idCardNo.trim();
				if (!value) {
					this.idCardError = '身份证不能为空';
				} else if (!idCardValidate(value)) {
					this.idCardError = '身份证输入有误，请重新输入';
				} else {
					this.idCardError = '';
				}
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = event.file[0];
				this.isCard = event.name;
				await this.uploadFilePromise(lists.url);
			},
			uploadFilePromise(imgUrl, row) {
				let token = uni.getStorageSync('token');
				const _this = this;
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						header: {
							authorization: 'Bearer ' + token
						},
						url: _this.baseUrl + '/common/upload', // 仅为示例，非真实的接口地址
						filePath: imgUrl,
						name: 'file',
						success: (res) => {
							const data = JSON.parse(res.data);
							if (_this.isCard == 1) {
								_this.formData.idCardJust = data.fileName;
							} else if (_this.isCard == 2) {
								_this.formData.idCardAgainst = data.fileName;
							} else if (_this.isCard == 3) {
								_this.formData.idCardRest = data.fileName;
							}
						}
					});
				});
			},
			getAddList() {
				console.log(this.formData);
				if (!this.isFormValid) {
					uni.showToast({
						title: '请将信息填写完善在点击保存',
						icon: 'error'
					});
					return;
				}
				my.updateUserId({
						realName: this.formData.realName,
						idCardNo: this.formData.idCardNo,
						idCardJust: this.formData.idCardJust,
						idCardAgainst: this.formData.idCardAgainst,
						idCardRest: this.formData.idCardRest
					})
					.then((res) => {
						if (res.code == 200) {
							console.log(res);
							uni.showToast({
								title: '保存成功！',
								icon: 'none'
							});
							setTimeout(() => {
								uni.navigateBack();
							}, 1500);
						}
					})
					.catch((erro) => {
						console.log(erro);
					});
			}
		},
		onLoad(o) {
			const user = uni.getStorageSync('user');
			console.log(user)
			this.formData.realName = user.realName;
			this.formData.idCardNo = user.idCardNo;
			this.formData.idCardJust = user.idCardJust;
			this.formData.idCardAgainst = user.idCardAgainst;
			// this.getAddList();
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background: linear-gradient(135deg, #e6f2ff, #bae6fd);
	}

	.error-text {
		color: #ff4545;
		font-size: 24rpx;
		margin-top: 10rpx;
	}

	.main-content {
		padding: 40rpx 30rpx 80rpx;
	}

	.main-top {
		padding-top: 100rpx;
		padding-bottom: 70rpx;
		background: linear-gradient(top, #2781ff, transparent);
	}

	.title-section {
		margin-bottom: 60rpx;
		text-align: center;
		color: #fff;

		.main-title {
			font-size: 40rpx;
			font-weight: bold;
			margin-bottom: 10rpx;
		}

		.sub-title {
			font-size: 32rpx;
			opacity: 0.9;
			margin-bottom: 30rpx;
		}

		.security-desc {
			padding: 24rpx 30rpx;
			border-radius: 16rpx;

			.desc-text {
				font-size: 28rpx;
				display: block;
				margin-bottom: 10rpx;
			}

			.required-text {
				font-size: 24rpx;
				color: #fff;
			}
		}
	}

	.input-section {
		background: rgba(255, 255, 255, 0.9);
		padding: 40rpx;
		border-radius: 20rpx;
		backdrop-filter: blur(10rpx);

		.input-item {
			position: relative;
			margin-bottom: 40rpx;

			.star {
				position: absolute;
				left: 30rpx;
				top: 50%;
				transform: translateY(-50%);
				color: #ff3b30;
				font-size: 28rpx;
			}
		}

		button {
			width: 300rpx;
			background-color: #2781ff;
			color: #fff;
			border-radius: 20rpx;
		}
	}

	/* uView组件样式覆盖 */
	.u-input {
		background: transparent !important;
		border-radius: 8rpx !important;
		padding-left: 60rpx !important;

		&::placeholder {
			color: #999 !important;
		}
	}

	.id-upload {
		margin: 30rpx 0 30rpx 0;
		display: flex;
		flex-direction: column;
		gap: 30rpx;

		.upload-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.upload-label {
			display: block;
			color: #666;
		}

		.upload-span {
			color: #a6a6a6;
			font-size: 23rpx;
		}
	}
</style>